<template>
  <div class='container'>
    陈志航
    <el-card>
      <page-tool>
        <div slot="before" class="search">
          <el-row>
            <el-col :span="4">学科名称</el-col>
            <el-col :span="10">
              <el-input v-model='queryInfo.subjectName' />
            </el-col>
            <el-col :span="10">
              <el-button class="clearSearch" @click="clean">清除</el-button>
              <el-button type="primary" @click="getSubjectList">搜索</el-button>
            </el-col>
          </el-row>
        </div>
        <div slot="after">
          <el-button type="success" icon="el-icon-edit" size="small" @click="addDialogShow = true">新增学科</el-button>
        </div>
      </page-tool>
      <!-- 提示部分 -->
      <el-alert :title="`数据一共${total}条`" type="info" show-icon :closable='false'>
      </el-alert>
      <!-- 表单部分 -->
      <el-table :data="subjectList">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="subjectName" label="学科名称"></el-table-column>
        <el-table-column prop="username" label="创建者"></el-table-column>
        <el-table-column label="创建日期">
          <template slot-scope="{row}">{{row.addDate | parseTimeByString }}</template>
        </el-table-column>
        <el-table-column prop="isFrontDisplay" label="前台是否显示">
          <template slot-scope="{row}">
            <span v-if="row.isFrontDisplay === 1">是</span>
            <span v-else>否</span>
          </template>
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录"></el-table-column>
        <el-table-column prop="tags" label="标签"></el-table-column>
        <el-table-column prop="totals" label="题目数量"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="{row}">
            <span class="operation"
              @click="$router.push({path:'/subjects/directorys',query:{id:row.id,name:row.subjectName}})">学科分类</span>
            <span class="operation"
              @click="$router.push({path:'/subjects/tags',query:{id:row.id,name:row.subjectName}})">学科标签</span>
            <span class="operation" @click="editSubject(row)">修改</span>
            <span class="operation" @click="delSubject(row)">删除</span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <el-row type="flex" justify="end">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
          :current-page="queryInfo.page" :page-sizes="[5, 10, 20, 50]" :page-size="queryInfo.pagesize" :total="total"
          layout="prev, pager, next, sizes, jumper">
        </el-pagination>
      </el-row>
    </el-card>

    <!-- 新增学科弹出框 -->
    <el-dialog :title='dialogTittle' :visible.sync='addDialogShow' width="30%" @close='btnCancel'>
      <el-form label-width="80px" :model="addForm" :rules="rules" ref="addFormRef">
        <el-form-item label="学科名字" prop="subjectName">
          <el-input v-model="addForm.subjectName" />
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch v-model="addForm.isFrontDisplay" active-color="#13ce66" inactive-color="#ff4949" :active-value='1'
            :inactive-value='0' />
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="end">
        <el-button @click="btnCancel">取消</el-button>
        <el-button type="primary" @click="btnOk">确认</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { list, add, detail, update, remove } from '@/api/hmmm/subjects.js'
export default {
  name: 'subjects',
  data () {
    return {
      // 学科列表
      subjectList: [],
      // 学科信息
      queryInfo: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      },
      total: 0,
      // 控制新增学科弹出框的显示与隐藏
      addDialogShow: false,
      // 添加表单的参数
      addForm: {
        subjectName: '',
        isFrontDisplay: 1
      },
      // 添加学科表单的验证规则
      rules: {
        subjectName: [{ required: true, message: '请输入学科名称', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 获取学科列表
    async getSubjectList () {
      const { data: res } = await list(this.queryInfo)
      this.subjectList = res.items
      this.total = res.counts
    },
    // 页面显示条数发生改变时的回调
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getSubjectList()
    },
    // 页码发生改变时触发的回调
    handleCurrentChange (newPage) {
      this.queryInfo.page = newPage
      this.getSubjectList()
    },
    // 点击添加学科
    btnOk () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        try {
          if (this.addForm.id) {
            // 处于编辑学科状态
            await update(this.addForm)
          } else {
            // 处于新增学科状态
            await add(this.addForm)
          }
          this.$message.success('操作成功！')
          this.getSubjectList()
          this.addDialogShow = false
        } catch (err) {
          this.$message.error('操作失败')
        }
      })
    },
    // 点击取消添加
    btnCancel () {
      this.addForm = {
        subjectName: '',
        isFrontDisplay: 1
      }
      this.$refs.addFormRef.resetFields()
      this.addDialogShow = false
    },
    async editSubject (data) {
      const res = await detail(data)
      this.addForm = res.data
      console.log(this.addForm)
      this.addDialogShow = true
    },
    async delSubject (data) {
      this.$confirm('确定要删除该学科吗').then(async () => {
        try {
          await remove(data)
          const { data: res } = await list(this.queryInfo)
          console.log(res)
          if (res.items.length === 0) {
            if (this.queryInfo.page !== 1) {
              this.queryInfo.page--
            } else {
              this.queryInfo.page = 1
            }
          }
          this.$message.success('删除成功')
          this.getSubjectList()
        } catch (err) {
          this.$message.error('删除失败')
        }
      }).catch(err => err)
    },
    clean () {
      this.queryInfo.subjectName = ''
      this.getSubjectList()
    }
  },
  created () {
    this.getSubjectList()
  },
  computed: {
    dialogTittle () {
      return this.addForm.id ? '编辑学科' : '新增学科'
    }
  }
}
</script>

<style scoped lang='less'>
.container {
  .search {
    font-size: 14px;
    font-weight: 700;
    color: #606266;
    line-height: 36px;
    .clearSearch {
      margin-left: 8px;
    }
  }
  .operation {
    padding: 0 10px;
    font-size: 14px;
    color: #409eff;
    cursor: pointer;
  }
  .el-alert {
    margin: 20px 0;
  }
  .el-pagination {
    margin-top: 20px;
  }
}
</style>
